<?php

namespace app\stuff\controller;


use app\stuff\model\Users as UsersModel;

use think\Controller;
use think\Session;
use think\Request;

class Index extends Controller
{

	public function index()
	{
		if (Session::has("name")) {
			Session::delete("name");
		}
		if (Session::has("CSUid")) {
			Session::delete("CSUid");
		}
		return $this->fetch('index');
	}

	public function captcha(Request $request)
	{
		$post = $request->post();
		return $this->validate($post, [
			'captcha|验证码' => 'require|captcha'
		]);
	}

	public function login(Request $request)
	{
		$post = $request->post();
		$result = $this->validate($post, [
			'captcha|验证码' => 'require|captcha'
		]);
		if ($result === true) {
			$user = new UsersModel();
			if ($user = $user->checkUser($post['CSUid'], $post['password'])) {
				Session::set('name', $user->name);
				Session::set('CSUid', $user->CSUid);
			} else {
				$result = "学号或密码错误!";
			}
		}
		return $result;
	}

	public function main(Request $request)
	{
		if (Session::has('name') && Session::has('CSUid')) {
			$name = $request->session('name');
			$CSUid = $request->session('CSUid');
			$users = new UsersModel();
			$check = $users->checkSession($name, $CSUid) ? true : false;
			if ($check) {
				$this->redirect('Main/index');
				return true;
			}
		}
		$this->redirect('index/index');
		return false;
	}
}
